﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BuildingForm.PersonInfo
{
    public partial class ManageBuildingRoom : Form
    {
        Maticsoft.DAL.Building buildingDao = new Maticsoft.DAL.Building();
        Maticsoft.DAL.Room roomDao = new Maticsoft.DAL.Room();
        Maticsoft.DAL.Ry_Stay_Info stayDao = new Maticsoft.DAL.Ry_Stay_Info();
        StayInfoManage manage;
        public ManageBuildingRoom(StayInfoManage man)
        {
            InitializeComponent();
            Init();
            if (man != null)
            {
                manage = man;
            }
        }

        private void Init()
        {
            this.txtBuilding.Text = "";
            this.txtRoom.Text = "";
            DataTable dt = buildingDao.GetList("").Tables[0];
            if (dt.Rows.Count > 0)
            {
                this.cboBuilding.DisplayMember = "BuildingName";
                this.cboBuilding.ValueMember = "BuildingName";
                this.cboBuilding.DataSource = dt;
            }
            DataTable dt2 = roomDao.GetList("").Tables[0];
            if (dt.Rows.Count > 0)
            {
                this.cboRoom.DisplayMember = "RoomName";
                this.cboRoom.ValueMember = "RoomName";
                this.cboRoom.DataSource = dt2;
            }
        }

        private void btnAddBuilding_Click(object sender, EventArgs e)
        {
            string str = this.txtBuilding.Text.ToString().Trim();
            if (string.IsNullOrEmpty(str))
            {
                MessageBox.Show("请输入信息！"); return;
            }
            DataTable dt = buildingDao.GetList("BuildingName='"+str+"'").Tables[0];
            if (dt.Rows.Count > 0)
            {
                MessageBox.Show("该信息已存在！"); return;
            }
            Maticsoft.Model.Building b = new Maticsoft.Model.Building();
            b.BuildingName = str;
            int num = buildingDao.Add(b);
            if (num > 0)
            {
                MessageBox.Show("添加数据成功！"); Init(); manage.Init();
            }
            else
            {
                MessageBox.Show("添加数据失败！");
            }
        }

        private void btnDelBuilding_Click(object sender, EventArgs e)
        {
            string str = this.cboBuilding.SelectedValue.ToString().Trim();
            if (string.IsNullOrEmpty(str))
            {
                MessageBox.Show("请选择要删除的信息！"); return;
            }
            DataTable dt = stayDao.GetList("wza='"+str+"'").Tables[0];
            if (dt.Rows.Count > 0)
            {
                MessageBox.Show("该信息已有关联数据，不能删除！"); return;
            }
            bool isT = buildingDao.DeleteByWhere("BuildingName='" + str + "'");
            if (isT)
            {
                MessageBox.Show("删除成功！"); Init(); manage.Init();
            }
            else
            {
                MessageBox.Show("删除失败！");
            }
        }

        private void btnAddRoom_Click(object sender, EventArgs e)
        {
            string str = this.txtRoom.Text.ToString().Trim();
            if (string.IsNullOrEmpty(str))
            {
                MessageBox.Show("请输入信息！"); return;
            }
            DataTable dt = roomDao.GetList("RoomName='" + str + "'").Tables[0];
            if (dt.Rows.Count > 0)
            {
                MessageBox.Show("该信息已存在！"); return;
            }
            Maticsoft.Model.Room r = new Maticsoft.Model.Room();
            r.RoomName = str;
            int num = roomDao.Add(r);
            if (num > 0)
            {
                MessageBox.Show("添加数据成功！"); Init(); manage.Init();
            }
            else
            {
                MessageBox.Show("添加数据失败！");
            }
        }

        private void btnDelRoom_Click(object sender, EventArgs e)
        {
            string str = this.cboRoom.SelectedValue.ToString().Trim();
            if (string.IsNullOrEmpty(str))
            {
                MessageBox.Show("请选择要删除的信息！"); return;
            }
            DataTable dt = stayDao.GetList("wzb='" + str + "'").Tables[0];
            if (dt.Rows.Count > 0)
            {
                MessageBox.Show("该信息已有关联数据，不能删除！"); return;
            }
            bool isT = roomDao.DeleteByWhere("RoomName='" + str + "'");
            if (isT)
            {
                MessageBox.Show("删除成功！"); Init(); manage.Init();
            }
            else
            {
                MessageBox.Show("删除失败！");
            }
        }

    }
}
